//
//  AppDelegate.m
//  Ev_Master
//
//  Created by Evan on 2019/6/10.
//  Copyright © 2019 Evan. All rights reserved.
//

#import "AppDelegate.h"
#import "PMTabBarController.h"
#import "IQKeyboardManager.h"
#import "DKProgressHUD.h"
#import "UserCenter.h"
#import "taskUserService.h"
#import "UIAlertController+Evan.h"

@interface AppDelegate ()

@end

@implementation AppDelegate

- (void)initView{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    
//    if(![[UserCenter shareCenter] isLogined]){
//        [[UserCenter shareCenter] clearUserState];
//    }
    
    UIViewController *vc = [UIViewController new];
    UIImageView *imageView = [[UIImageView alloc]initWithFrame:self.window.bounds];
    imageView.image = [UIImage imageNamed:@"statrtBG"];
    [vc.view addSubview:imageView];
    self.window.rootViewController = vc;
    
//    PMTabBarController *tb = [[PMTabBarController alloc] init];
//    self.window.rootViewController = tb;
    
    //检查版本
    [self checkVersionWithCallback:^{
        // 正常启动
        // 初始化一个tabBar控制器
        PMTabBarController *tb = [[PMTabBarController alloc] init];
        self.window.rootViewController = tb;
    }];
    
    
    //设置Window为主窗口并显示出来
    [self.window makeKeyAndVisible];
    [self setIQKeyboard];
    //    [NSThread sleepForTimeInterval:3];
}


- (void)setIQKeyboard
{
    IQKeyboardManager *manager = [IQKeyboardManager sharedManager];
    manager.enable = YES;
    manager.shouldResignOnTouchOutside = YES;
    manager.shouldToolbarUsesTextFieldTintColor = YES;
    manager.enableAutoToolbar = NO;
}

- (void)checkVersionWithCallback:(void(^)(void))callback{
    [DKProgressHUD showLoadingWithStatus:@"检查当前版本"];
    NSMutableDictionary *dict = [NSMutableDictionary new];
     NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
     // app版本
    NSString *app_Version = [infoDictionary objectForKey:@"CFBundleShortVersionString"];
        [dict setValue:app_Version forKey:@"version"];
        [dict setValue:@"ios" forKey:@"app-type"];
    [taskUserService getVersionInfo:dict success:^(modelVersionData * _Nonnull response, BOOL success) {
        //判断你需要更新的业务逻辑
        callback();
    } failture:^(NSError * _Nonnull error, NSString * _Nonnull strDescription) {
                
        if (error.code == 1000) {
            [[UIAlertController alloc]alertTitle:@"版本升级" alertMessage:strDescription  leftCancelTitle:@"退出" leftCancel:^(UIAlertAction *action) {
                exit(0);
            } rightTitle:@"立即升级" rightOK:^(UIAlertAction *action) {
                
                NSLog(@"你需要升级了");
                NSDictionary *dict = error.userInfo;
                NSString *textURL = [[dict valueForKey:@"data"] valueForKey:@"url"];
                NSURL *cleanURL = [NSURL URLWithString:[NSString stringWithFormat:@"%@", textURL]];
                [[UIApplication sharedApplication] openURL:cleanURL];
                
            }];
            return;
        }
        //        }
        callback();
        [DKProgressHUD dismiss];
        [DKProgressHUD showErrorWithStatus:strDescription];
    }];
}


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    
    [self initView];
    return YES;
}


- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
}


- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}


- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
}


- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}


- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}


@end
